package org.example.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.demo.constant.MessageConstant;
import org.example.demo.entity.TrainingLog;
import org.example.demo.service.TrainingLogService;
import org.example.demo.util.AjaxResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 培训日志控制器
 */
@RestController
@RequestMapping("trainingLog")
public class TrainingLogController {

    @Resource
    private TrainingLogService trainingLogService;

    /**
     * 分页查询培训日志
     * 
     * @param trainingLog 查询条件
     * @param pageNum     页码
     * @param pageSize    每页大小
     * @return 查询结果
     */
    @PostMapping("page")
    public AjaxResult page(@RequestBody TrainingLog trainingLog,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<TrainingLog> page = trainingLogService.selectPage(trainingLog, pageNum, pageSize);

        Map<String, Object> result = new HashMap<>();
        result.put("page", page);
        result.put("total", page.getTotal());

        return AjaxResult.success(result);
    }

    /**
     * 根据ID查询培训日志
     * 
     * @param id 培训日志ID
     * @return 查询结果
     */
    @GetMapping("{id}")
    public AjaxResult getById(@PathVariable Long id) {
        TrainingLog trainingLog = trainingLogService.getById(id);

        if (trainingLog != null) {
            return AjaxResult.success(trainingLog);
        } else {
            return AjaxResult.error("培训日志不存在");
        }
    }

    /**
     * 新增培训日志
     * 
     * @param trainingLog 培训日志信息
     * @return 操作结果
     */
    @PostMapping
    public AjaxResult add(@RequestBody TrainingLog trainingLog) {
        trainingLog.setCreateTime(LocalDateTime.now());
        trainingLog.setUpdateTime(LocalDateTime.now());

        if (trainingLogService.save(trainingLog)) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error(MessageConstant.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 修改培训日志
     * 
     * @param trainingLog 培训日志信息
     * @return 操作结果
     */
    @PutMapping
    public AjaxResult update(@RequestBody TrainingLog trainingLog) {
        trainingLog.setUpdateTime(LocalDateTime.now());

        if (trainingLogService.updateById(trainingLog)) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error(MessageConstant.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 删除培训日志
     * 
     * @param id 培训日志ID
     * @return 操作结果
     */
    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable Long id) {
        if (trainingLogService.removeById(id)) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error(MessageConstant.INTERNAL_SERVER_ERROR);
        }
    }
}